<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;

//前台登陆控制器
class LoginController extends Controller{
	private $model;
	public function __init(){
	   //前台用户表
	   $this->model = new \Common\Model\home_user;
	}
    //注册
    public function register(){
    	if(IS_POST){
			if(!$this->model->store()) View::error($this->model->getError());
			View::success('注册成功',U('login'));
    	}
    	//默认模板
       View::make();
    }
	
	//验证码
	public function Code(){
		Code::make();
	}
	
	//登录
	public function login(){
		if(IS_POST){
			//获得登陆提交的账号密码
			$username = Q('post.username');
			$password = Q('post.password','','md5');
			//获得全部用户数据
			$data = $this->model->where("username='{$username}'")->find();
		    //判断账户名和密码是否正确
			if($data['username'] != $username || $data['password'] != $password) View::error('账号密码错误');
			$_SESSION['uid'] = $data['uid'];
			$_SESSION['username'] = $data['username'];
			View::success('登陆成功',U('Index/index'));
		}
		//默认模板
	    View::make();
	}
	
	//退出登录
	public function Logout(){
		session_unset();
		session_destroy();
		View::success('退出成功',U('Index/index'));
	}
	
	
	
	
	
}
